<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>

    </body>
</html>
<script>
    var p1 = {
        name: "胡培珉",
        age: 20,
        score: 100,
        gender: "男"
    };

    // p1['name'] p1['age'] p1.name p1.age


    var p2 = {
        name: "程超强",
        age: 26,
        score: 88,
        gender: "男"
    };

    var p3 = {
        name: "许道广",
        age: 25,
        score: 66,
        gender: "男"
    };

    var p4 = {
        name: "王争艳",
        age: 18,
        score: 120,
        gender: "班花"
    };

    var pArr = [p1, p2, p3, p4];

    function printInfo(pArr) {
        for (var i = 0; i < pArr.length; i++) {
            console.log(pArr[i].name, pArr[i].age, pArr[i].gender, pArr[i].score);
        }


    }

    // printInfo(pArr);


    function sortByAge(pArr, key, flag) {

        for (var i = 0; i < pArr.length - 1; i++) {
            for (var j = 0; j < pArr.length - 1 - i; j++) {
                // 按分数升序排列
                /*
                var bool = pArr[j][key] > pArr[j+1][key];
                bool = flag ? !bool : bool;
                if (bool) {
                    var temp = pArr[j];
                    pArr[j] = pArr[j+1];
                    pArr[j+1] = temp;
                }
                */
                if (flag) {
                    if (pArr[j][key] < pArr[j+1][key]) {
                        var temp = pArr[j];
                        pArr[j] = pArr[j+1];
                        pArr[j+1] = temp;
                    }
                } else {
                    if (pArr[j][key] > pArr[j+1][key]) {
                        var temp = pArr[j];
                        pArr[j] = pArr[j+1];
                        pArr[j+1] = temp;
                    }
                }

            }
        }

    }
    // 排序
    sortByAge(pArr, 'score', true);
    printInfo(pArr);








</script>